// 信誉分管理工具类
export interface ScoreChange {
  id: string
  type: 'increase' | 'decrease'
  score: number
  title: string
  description: string
  timestamp: number
  category: 'task' | 'exam' | 'system'
}

export interface UserScoreData {
  currentScore: number
  initialScore: number
  history: ScoreChange[]
}

export class ReputationScoreManager {
  private static readonly INITIAL_SCORE = 80
  private static readonly MIN_SCORE_FOR_TASKS = 60

  // 任务相关分数规则
  private static readonly TASK_RULES = {
    COMPLETE_WITH_PRAISE: 2,    // 按时完成且获得好评
    FAIL_TO_COMPLETE: -2,       // 未能按时完成
    DELAY_PER_DAY: -1,          // 每拖延一天
  }

  // 考试相关分数规则
  private static readonly EXAM_RULES = {
    EXCELLENT: 3,               // 90分以上
    GOOD: 0,                    // 70-89分
    WARNING: -2,                // 60-69分
    FAIL: -5,                   // 低于60分
  }

  /**
   * 计算任务完成后的信誉分变化
   * @param isCompleted 是否按时完成
   * @param hasPraise 是否获得好评
   * @param delayDays 延期天数
   * @returns 分数变化
   */
  static calculateTaskScoreChange(
    isCompleted: boolean,
    hasPraise: boolean = false,
    delayDays: number = 0
  ): number {
    let scoreChange = 0

    if (isCompleted) {
      if (hasPraise) {
        scoreChange += this.TASK_RULES.COMPLETE_WITH_PRAISE
      }
    } else {
      scoreChange += this.TASK_RULES.FAIL_TO_COMPLETE
    }

    // 延期扣分
    if (delayDays > 0) {
      scoreChange += this.TASK_RULES.DELAY_PER_DAY * delayDays
    }

    return scoreChange
  }

  /**
   * 计算考试后的信誉分变化
   * @param score 考试分数
   * @returns 分数变化
   */
  static calculateExamScoreChange(score: number): number {
    if (score >= 90) {
      return this.EXAM_RULES.EXCELLENT
    } else if (score >= 70) {
      return this.EXAM_RULES.GOOD
    } else if (score >= 60) {
      return this.EXAM_RULES.WARNING
    } else {
      return this.EXAM_RULES.FAIL
    }
  }

  /**
   * 检查是否可以接取任务
   * @param currentScore 当前信誉分
   * @returns 是否可以接取任务
   */
  static canTakeTask(currentScore: number): boolean {
    return currentScore >= this.MIN_SCORE_FOR_TASKS
  }

  /**
   * 获取信誉分等级
   * @param score 信誉分
   * @returns 等级信息
   */
  static getScoreLevel(score: number): {
    level: 'excellent' | 'good' | 'normal' | 'warning' | 'danger'
    status: string
    color: string
  } {
    if (score >= 90) {
      return { level: 'excellent', status: '优秀', color: '#52c41a' }
    } else if (score >= 80) {
      return { level: 'good', status: '良好', color: '#4A90E2' }
    } else if (score >= 70) {
      return { level: 'normal', status: '一般', color: '#faad14' }
    } else if (score >= 60) {
      return { level: 'warning', status: '警告', color: '#fa8c16' }
    } else {
      return { level: 'danger', status: '危险', color: '#f5222d' }
    }
  }

  /**
   * 创建任务完成记录
   * @param taskName 任务名称
   * @param isCompleted 是否完成
   * @param hasPraise 是否获得好评
   * @param delayDays 延期天数
   * @returns 分数变化记录
   */
  static createTaskRecord(
    taskName: string,
    isCompleted: boolean,
    hasPraise: boolean = false,
    delayDays: number = 0
  ): ScoreChange {
    const scoreChange = this.calculateTaskScoreChange(isCompleted, hasPraise, delayDays)
    
    let title = ''
    let description = ''
    
    if (isCompleted) {
      if (hasPraise) {
        title = '任务完成获得好评'
        description = `按时完成了"${taskName}"任务，企业给予好评`
      } else {
        title = '任务完成'
        description = `完成了"${taskName}"任务`
      }
    } else {
      title = '任务未能按时完成'
      description = `"${taskName}"任务未能按时完成`
    }

    if (delayDays > 0) {
      title = '任务延期'
      description = `"${taskName}"任务延期${delayDays}天`
    }

    return {
      id: this.generateId(),
      type: scoreChange >= 0 ? 'increase' : 'decrease',
      score: Math.abs(scoreChange),
      title,
      description,
      timestamp: Date.now(),
      category: 'task'
    }
  }

  /**
   * 创建考试记录
   * @param examName 考试名称
   * @param score 考试分数
   * @returns 分数变化记录
   */
  static createExamRecord(examName: string, score: number): ScoreChange {
    const scoreChange = this.calculateExamScoreChange(score)
    
    let title = ''
    let description = ''
    
    if (score >= 90) {
      title = '考试高分通过'
      description = `${examName}获得${score}分`
    } else if (score >= 70) {
      title = '考试通过'
      description = `${examName}获得${score}分`
    } else if (score >= 60) {
      title = '考试及格'
      description = `${examName}获得${score}分`
    } else {
      title = '考试不及格'
      description = `${examName}获得${score}分，需要重新考试`
    }

    return {
      id: this.generateId(),
      type: scoreChange >= 0 ? 'increase' : 'decrease',
      score: Math.abs(scoreChange),
      title,
      description,
      timestamp: Date.now(),
      category: 'exam'
    }
  }

  /**
   * 生成唯一ID
   * @returns 唯一ID
   */
  private static generateId(): string {
    return Date.now().toString(36) + Math.random().toString(36).substr(2)
  }

  /**
   * 获取初始信誉分
   * @returns 初始信誉分
   */
  static getInitialScore(): number {
    return this.INITIAL_SCORE
  }

  /**
   * 获取最低任务接取分数
   * @returns 最低分数
   */
  static getMinTaskScore(): number {
    return this.MIN_SCORE_FOR_TASKS
  }

  /**
   * 格式化时间
   * @param timestamp 时间戳
   * @returns 格式化后的时间字符串
   */
  static formatTime(timestamp: number): string {
    const date = new Date(timestamp)
    const now = new Date()
    const diff = now.getTime() - date.getTime()
    
    if (diff < 60000) return '刚刚'
    if (diff < 3600000) return `${Math.floor(diff / 60000)}分钟前`
    if (diff < 86400000) return `${Math.floor(diff / 3600000)}小时前`
    if (diff < 2592000000) return `${Math.floor(diff / 86400000)}天前`
    
    return date.toLocaleDateString()
  }

  /**
   * 验证信誉分是否有效
   * @param score 信誉分
   * @returns 是否有效
   */
  static isValidScore(score: number): boolean {
    return score >= 0 && score <= 100
  }

  /**
   * 获取信誉分建议
   * @param currentScore 当前信誉分
   * @returns 建议列表
   */
  static getScoreSuggestions(currentScore: number): string[] {
    const suggestions: string[] = []
    
    if (currentScore < 60) {
      suggestions.push('信誉分过低，无法接取新任务')
      suggestions.push('建议认真完成现有任务，提升信誉分')
      suggestions.push('参加反作弊考试，争取高分')
    } else if (currentScore < 70) {
      suggestions.push('信誉分偏低，需要提升')
      suggestions.push('按时完成任务，争取获得好评')
      suggestions.push('避免任务延期')
    } else if (currentScore < 80) {
      suggestions.push('信誉分一般，有提升空间')
      suggestions.push('保持良好表现，争取更高分数')
    } else if (currentScore < 90) {
      suggestions.push('信誉分良好，继续保持')
      suggestions.push('可以尝试更有挑战性的任务')
    } else {
      suggestions.push('信誉分优秀，是值得信赖的用户')
      suggestions.push('可以优先获得高质量任务')
    }
    
    return suggestions
  }
} 